<template>
  <div class="box" style="height: 500px">
     <!--  @mouseenter="mouseEnter"鼠标停留时事件 -->
     <!-- @mouseleave="mouseleave"鼠标移除事件 -->
    <div class="xtx-slider"
    @mouseenter="mouseEnter"
    @mouseleave="mouseleave">
      <ul class="slider-body">
        <li
          class="slider-item"
          v-for="(item, i) in sliders"
          :key="i"
          :class="{ fade: index === i }"
        >
          <img :src="item.imgUrl" />
        </li>
      </ul>
      <!-- 放置可点击交互切换按钮 -->
      <div class="slider-indicator">
        <span
          @click="indicatorClick(i)"
          v-for="(item,i)  in sliders"
          :key="item.id"
          :class="{ active: index === i }"
        ></span>
      </div>
    </div>
  </div>
</template>

<script>
/*
点击按钮切换图片
思路：点击按钮的时候记录一下当前点击得下标值
由于图片得按钮的个数是相同的下标值一一对应，所以可以通过判断
当前记录的index
*/
export default {
  name: 'XtxSlider',
  props: {
    sliders: {
      type: Array,
      // 复杂类型的时候必须通过函数返回一个默认值
      default () {
        return []
      }
    },
    time: {
      type: Number,
      default: 1500
    },
    isLoop: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      index: 0, // 记录当前点击按钮下标值
      timer: null // 记录当前定时器
    //   sliders: [
    //     {
    //       hrefUrl: '/#/',
    //       id: '630000201807155635',
    //       imgUrl: 'http://zhoushugang.gitee.io/erabbit-client-pc-static/images/b1.jpg'
    //     },
    //     {
    //       hrefUrl: '/#/',
    //       id: '650000201207058528',
    //       imgUrl: 'http://zhoushugang.gitee.io/erabbit-client-pc-static/images/b2.jpg'
    //     }
    //   ]
    }
  },
  methods: {
    //   点击按钮切换轮播
    indicatorClick (index) {
      this.index = index
    },
    // 自动轮播
    initLoop () {
      this.timer = window.setInterval(() => {
        // 修改index值，自增修改
        this.index++
        // 限制index得大小
        // 如果index大于this.slider得长度就减一，避免自动停播让它等于0，继续开始
        if (this.index > this.sliders.length - 1) { this.index = 0 }
      }, this.time)
    },
    // 鼠标经过暂停
    mouseEnter () {
      window.clearInterval(this.timer)
    },
    // 鼠标移除恢复轮播
    mouseleave () {
      this.initLoop()
    }
  },
  //   清除定时器
  Unmount () {
    // 清除定时器
    window.clearInterval(this.timer)
  },
  //   是否开启轮播
  mounted () {
    this.isLoop && this.initLoop()
  }
}
</script>

<style scoped lang='less'>
.xtx-slider {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .slider {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .slider-btn {
      opacity: 1;
    }
  }
}
</style>
